﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ChatIBOXClient
{
    public enum CallbackType {RefreshClient, Receive, UserJoin, UserLeave, HasNotify };
    // Tao lop ProxyEventArgs
    public class ProxyEventArgs : EventArgs
    {

    }

    // Tao cac tham so cho ProxyCallbackEventArgs
    public class ProxyCallbackEventArgs : EventArgs
    {

        public CallbackType callbackType;
        public ChatIBOXClient.ServiceReference.Client[] listClient = null;
        public ChatIBOXClient.ServiceReference.Client client = new ChatIBOXClient.ServiceReference.Client();
        public ChatIBOXClient.ServiceReference.Message message = new ChatIBOXClient.ServiceReference.Message();
        public string notify = "ntf";
    }

    public class ProxySingleton : ChatIBOXClient.ServiceReference.IChatServiceCallback
    {
        private static ProxySingleton proxySingleton = null;
        private ServiceReference.ChatServiceClient proxy = null;
        public ServiceReference.Client receiver = null;
        public ServiceReference.Client localClient = null;

        
        // Callback proxy event
        public delegate void ProxyCallbackEventHander(object sender, ProxyCallbackEventArgs e);
        public event ProxyCallbackEventHander ProxyCallbackEvent;

        // Main proxy event
        private delegate void ProxyEventHander(object sender, ProxyEventArgs e);
        private event ProxyEventHander ProxyCEvent;

        // Get instance of ProxySingleton
        public static ProxySingleton GetInstance()
        {
            if (proxySingleton == null)
            {
                proxySingleton = new ProxySingleton();
            }

            return proxySingleton;
        }

        #region Public method
        public void Connect()
        {
            InstanceContext context = new InstanceContext(this);
            proxy = new ChatIBOXClient.ServiceReference.ChatServiceClient(context);
            proxy.ConnectAsync(localClient);
        }

        public void Whisper(ServiceReference.Message message, ServiceReference.Client receiver )
        {
            proxy.WhisperAsync(message, receiver);
        }
        #endregion

        private void OnProxyCallbackEvent(ProxyCallbackEventArgs e)
        {
            if (ProxyCallbackEvent != null)
            {
                ProxyCallbackEvent(this, e);
            }
        }

        #region IChatServiceCallback Members

        public void RefreshClients(ChatIBOXClient.ServiceReference.Client[] clients)
        {
            ProxyCallbackEventArgs e = new ProxyCallbackEventArgs();
            e.callbackType = CallbackType.RefreshClient;
            e.message = new ChatIBOXClient.ServiceReference.Message();
            e.message.Content = "OK";
            e.client = new ChatIBOXClient.ServiceReference.Client();
            e.listClient = clients;
            OnProxyCallbackEvent(e);
        }

        public void UserJoint(ChatIBOXClient.ServiceReference.Client client)
        {
            ProxyCallbackEventArgs e = new ProxyCallbackEventArgs();
            e.callbackType = CallbackType.UserJoin;
            e.client = client;
            OnProxyCallbackEvent(e);
        }

        public void UserLeave(ChatIBOXClient.ServiceReference.Client client)
        {
            throw new NotImplementedException();
        }

        public void Receive(ChatIBOXClient.ServiceReference.Message content)
        {
            ProxyCallbackEventArgs e = new ProxyCallbackEventArgs();
            e.message = new ChatIBOXClient.ServiceReference.Message();
            e.message = content;

            e.client = new ChatIBOXClient.ServiceReference.Client();
            e.callbackType = CallbackType.Receive;
            OnProxyCallbackEvent(e);
        }
        public void Disconnect()
        {
            proxy.DisconnectAsync(localClient);
        }
        #endregion

        #region IChatServiceCallback Members


        public IAsyncResult BeginRefreshClients(ChatIBOXClient.ServiceReference.Client[] clients, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndRefreshClients(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        public IAsyncResult BeginUserJoint(ChatIBOXClient.ServiceReference.Client client, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndUserJoint(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        public IAsyncResult BeginUserLeave(ChatIBOXClient.ServiceReference.Client client, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndUserLeave(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        public IAsyncResult BeginReceive(ChatIBOXClient.ServiceReference.Message content, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndReceive(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IChatServiceCallback Members


        public void HasNotify(string content)
        {
            ProxyCallbackEventArgs e = new ProxyCallbackEventArgs();
            e.notify = content;
            e.callbackType = CallbackType.HasNotify;
            OnProxyCallbackEvent(e);
        }

        public IAsyncResult BeginHasNotify(string content, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndHasNotify(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
